/*
 * Copyright (c) 2002-2003 by OpenSymphony
 * All rights reserved.
 */
package org.jackysoft.opensymphony.workflow.entity;

import java.util.ArrayList;
import java.util.List;

import org.jackysoft.util.ValidationUtils;


/**
 * @author <a href="mailto:plightbo@hotmail.com">Pat Lightbody</a>
 */

public class ConditionalResultDescriptor extends ResultDescriptor {
    //~ Instance fields ////////////////////////////////////////////////////////

    /**
	 * 
	 */
	private static final long serialVersionUID = -9101546025871639419L;
	protected List conditions = new ArrayList();

    //~ Constructors ///////////////////////////////////////////////////////////

   
	public ConditionalResultDescriptor(){}
	
    public ConditionalResultDescriptor(org.dom4j.Element element) {
    	super(element);
    	this.init(element);
    }

    //~ Methods ////////////////////////////////////////////////////////////////

   
    public List getConditions() {
        return conditions;
    }

    
    public String getDestination() {
        WorkflowDescriptor desc = null;
        String sName = "";
        AbstractDescriptor actionDesc = getParent().getParent();

        if (actionDesc != null) {
            desc = (WorkflowDescriptor) actionDesc.getParent();
        }

        if (join != 0) {
            return "join #" + join;
        } else if (split != 0) {
            return "split #" + split;
        } else {
            if (desc != null) {
                sName = desc.getStep(step).getName();
            }

            return "step #" + step + " [" + sName + "]";
        }
    }

    public void validate() throws InvalidWorkflowDescriptorException {
        super.validate();

        if (conditions.size() == 0) {
            throw new InvalidWorkflowDescriptorException("Conditional result from " + ((ActionDescriptor) getParent()).getName() + " to " + getDestination() + " must have at least one condition");
        }

        ValidationUtils.validate(conditions);
    }
 
        
    @Override
    protected void init(org.dom4j.Element conditionalResult) {
    	super.init(conditionalResult);
    	//Iterator citr = conditionalResult.elementIterator("conditions");
    	conditions = new ArrayList();
    	buildElement(conditionalResult, "conditions", "condition", ConditionDescriptor.class, this.conditions, this);
 	    
    }
}
